<?php
namespace app\home\controller;
use app\home\controller\Common;
use app\home\model\Article;
use think\Db;
use Qcloud\Sms\SmsSingleSender;
use excel\Excel;
class Index extends Common
{
    function test(){
        echo 22222;
        // require "vendor/qcloudsms_php/src/index.php";
        // $ssender = new SmsSingleSender('1400191740','0eb1559d1dcd2b20e9ef7be526ebb1d6');
    }
    public function index()
    {
        $condition = 'is_del=0 AND is_hide=0 AND is_home=1';
        $page = intval(input('page'));
        $page = $page>0 ? $page : 1;    // 当前页数
        $num = 10;

        $wd = input("get.wd");
        if($wd != ''){
            $condition .= " AND (title like '%".$wd."%' or keywords like '%".$wd."%')";
        }

        $count = Db::name('article')->where($condition)->count();// 查询满足要求的总记录数
        $list = Db::name('article')->where($condition)->order('add_time desc')->limit($num*($page-1),$num)->select();
        // 是否有下一页
        $next_page_num = Db::name('article')->where($condition)->order('add_time desc')->limit($num*$page,$num)->select();
        $has_next_page = empty($next_page_num) ? 0 : 1;

        if(request()->isAjax()){
            $data['tbody'] = $this->getTbody($list);
            $data['has_next_page'] = $has_next_page;

            return json($data);
        }else{
            $this->assign('list', $list);
            $this->assign('has_next_page', $has_next_page);
            $this->assign('wd', $wd);

            $ads = Db::name('ads')->where('id=1 and is_hide=0')->find();
            $this->assign('banner',json_decode($ads['content'],true));

            // 新鲜事
            $xxs_condition = "is_del=0 AND is_hide=0 AND is_tj=1 AND is_home=1";
            $xxs_list = Db::name('article')->where($xxs_condition)->field('article_id,title')->order('add_time desc')->limit(5)->select();
            $this->assign('xxs_list', $xxs_list);
            // 阅读排行榜
            $ydph_condition = "is_del=0 AND is_hide=0 AND is_home =1";
            $ydph_list = Db::name('article')->where($ydph_condition)->field('article_id,title')->order('click desc')->limit(8)->select();
            $this->assign('ydph_list', $ydph_list);

            $title = '优生活-生活干货,健康分享';
            $keywords = $this->config['keyword'];
            $description = $this->config['description'];
            $this->assign('title',$title);
            $this->assign('keywords',$keywords);
            $this->assign('description',$description);

            return view();
        }
        
    }

    function classify(){
        $class_url = $_SERVER['REQUEST_URI'];
        
        $condition = 'is_del=0 AND is_hide=0';
        if(strpos($class_url, 'tuijian/') == 1){
            $class_url = '/tuijian/';
            $condition .= ' AND is_tuijian=1';
            $className = '推荐';
        }elseif(strpos($class_url, 'ganhuo/') ==1){
            $class_url = '/ganhuo/';
            $condition .= ' AND is_ganhuo=1';
            $className = '干货';
        }elseif(strpos($class_url, 'duanzi/') ==1){
            $class_url = '/duanzi/';
            $className = '段子';
            $condition .= ' AND is_duanzi=1';
        }else{
            $this->redirect("/");
        }
        $class_name = str_replace('/', '', $class_url);

        $page = intval(input('page'));
        $page = $page>0 ? $page : 1;    // 当前页数
        $num = 10;
        $count = Db::name('article')->where($condition)->count();// 查询满足要求的总记录数
        $list = Db::name('article')->where($condition)->order('sort_order,add_time desc')->limit($num*($page-1),$num)->select();
        // 是否有下一页
        $next_page_num = Db::name('article')->where($condition)->order('sort_order,add_time desc')->limit($num*$page,$num)->select();
        $has_next_page = empty($next_page_num) ? 0 : 1;

        if(request()->isAjax()){
            $data['tbody'] = $this->getTbody($list);
            $data['has_next_page'] = $has_next_page;

            return json($data);
        }else{
            //前台数据
            $title = $className.'-'.$this->config['site_name'];
            $this->assign('title',$title);
            $this->assign('keywords',$this->config['keywords']);
            $this->assign('description',$this->config['description']);

            $this->assign('class_url', $class_url);
            $this->assign('className', $className);
            $this->assign('class_name', $class_name);
            $this->assign('list', $list);
            $this->assign('has_next_page', $has_next_page);

            // 新鲜事
            $xxs_condition = "is_del=0 AND is_hide=0 AND is_tj=1 AND is_{$class_name} =1";
            $xxs_list = Db::name('article')->where($xxs_condition)->field('article_id,title')->order('add_time desc')->limit(5)->select();
            $this->assign('xxs_list', $xxs_list);
            // 阅读排行榜
            $ydph_condition = "is_del=0 AND is_hide=0 AND is_{$class_name} =1";
            $ydph_list = Db::name('article')->where($ydph_condition)->field('article_id,title')->order('click desc')->limit(8)->select();
            $this->assign('ydph_list', $ydph_list);

            return view();
        }
    }

    function getTbody($list){
        $tbody = '';
        foreach($list as $k=>$v){
            $addTime = date('Y.m.d', strtotime($v['add_time']));
            $thisUrl = url('index/article',array('id'=>$v['article_id']));
            if(empty(trim($v['sub_title']))){
                $subTitle = subtext(movehtml($v['content']), 60);
            }else{
                $subTitle = $v['sub_title'];
            }
            if(empty(trim($v['thumb']))){
                $tbody .= '<section class="list">
                  <h2 class="mucctitle"><a href="'.$thisUrl.'" target="_blank">'.$v['title'].'</a></h2>';
                $tbody .='<time class="tebe ">'.$addTime;
                if($v['is_hot'] == 1){
                    $tbody .= '<span class="zan">Hot</span>';
                }
                $tbody .= '</time>';
                $tbody .='<div class="zuiyao"><p>'.$subTitle.'</p></div></section>';
            }else{
                $tbody .= '<section class="list">
                  <h2 class="mecctitle"><a href="'.$thisUrl.'" target="_blank">'.$v['title'].'</a></h2>';
                $tbody .= '<span class="titleimg"><a href="'.$thisUrl.'" target="_blank"><img width="1000" height="556" src="'.$v['thumb'].'"
                  class="attachment-thumbnail size-thumbnail wp-post-image" alt=""/></a></span>';
                $tbody .= '<time class="">'.$addTime;
                if($v['is_hot'] == 1){
                    $tbody .= '<span class="zan">Hot</span>';
                }
                $tbody .= '</time>';
                $tbody .='<div class="zaiyao"><p>'.$subTitle.'</p></div></section>';
            }
        }

        return $tbody;
    }

    //文章栏目
    function category(){
        $kid = input('route.id');
        $this->assign('kind_id',$kid);
        if(!$kid || !is_numeric($kid)) $this->error('地址错误','/');
        //处理分类
        $get_kind = Db::name('article_kind')->where("is_hide=0 and kind_id={$kid}")->find();
        if(!$get_kind) $this->error('地址错误','/');
        $this->assign('get_kind',$get_kind);
        //获得栏目分类
        $kind_data = Article::kind_data($kid);
        $top_kind = $kind_data['top_kind'];
        $two_kind = $kind_data['two_kind'];
        $two_kind_list = $kind_data['two_kind_list'];
        $three_kind = $kind_data['three_kind'];
        $three_kind_list = $kind_data['three_kind_list'];

        //调用哪一级模板
        $kind = $get_kind;
        if($kind['kind_template'] == ''){
            $kind = Db::name('article_kind')->where("parent_id=".$get_kind['kind_id'])->order("sort_order")->find();
        }
        if(empty($kind['kind_template'])){
            $this->redirect("/");
        }
        
        $listid[] = $kid;
        $listid = findArtNextKind($kid,$listid);
        $page = intval(input('page'));
        $page = $page>0 ? $page : 1;    // 当前页数
        $get_condition = "is_del=0 AND is_hide=0";
        $get_condition .= " AND kind_id in (".implode(',',$listid).")";
        $get_count = Db::name('article')->where($get_condition)->count();// 查询满足要求的总记录数
        $get_num = $get_kind['art_num']?$get_kind['art_num']:10;
        $get_list = Db::name('article')->where($get_condition)->order('sort_order,add_time desc')->limit($get_num*($page-1),$get_num)->select();
        $get_one = $get_list[0];
        // 是否有下一页
        $next_page = Db::name('article')->where($get_condition)->order('sort_order,add_time desc')->limit($get_num*$page,$get_num)->select();
        $has_next_page = empty($next_page) ? 0 : 1;
        if(request()->isAjax()){
            $data['tbody'] = $this->getTbody($get_list);
            $data['has_next_page'] = $has_next_page;

            return json($data);
        }else{
            $this->assign('top_kind',$top_kind);
            $this->assign('two_kind_list',$two_kind_list);
            $this->assign('two_kind',$two_kind);
            $this->assign('three_kind',$three_kind);
            $this->assign('three_kind_list',$three_kind_list);

            if(strpos($kind['keywords'], '|')){
                $kind['hotword'] = explode('|', $kind['keywords']);
            }
            
            $this->assign('kid',$kid);
            $this->assign('kind',$kind);

            //前台数据
            if($get_kind['web_title']<>''){
                $title = $get_kind['web_title'];
            }else{
                $title = $get_kind['kind_name'].'-'.$this->config['site_name'];
            }
            $keywords = $get_kind['keywords'];
            $description = $get_kind['description'];
            $this->assign('title',$title);
            $this->assign('keywords',$keywords);
            $this->assign('description',$description);
            $this->assign('list',$get_list);
            $this->assign('has_next_page',$has_next_page);

            // 新鲜事
            $xxs_condition = "is_del=0 AND is_hide=0 AND is_tj=1 AND kind_id in (".implode(',',$listid).")";
            $xxs_list = Db::name('article')->where($xxs_condition)->field('article_id,title')->order('add_time desc')->limit(5)->select();
            $this->assign('xxs_list', $xxs_list);
            // 阅读排行榜
            $ydph_condition = "is_del=0 AND is_hide=0 AND kind_id in (".implode(',',$listid).")";
            $ydph_list = Db::name('article')->where($ydph_condition)->field('article_id,title')->order('click desc')->limit(8)->select();
            $this->assign('ydph_list', $ydph_list);

            return view($kind['kind_template']);
        }

    }

    function more_article(){
        if(request()->isAjax()){
            // var_dump(I('post.'));exit;
            $kind_id = input('post.kind_id');
            $p = input('post.p');
            $art_num = input('post.art_num');
            $condition = "kind_id={$kind_id} AND is_del=0";
            $data = Db::name('article')->where($condition)->order('sort_order,add_time desc')->limit($art_num*$p,$art_num)->select();
            // var_dump($data);exit;
            
            if($data){
                foreach($data as $k=>$v){
                    $str = '';
                    $label = explode(',',$v['map']);
                    foreach($label as $kk=>$vv){
                        $str.="<a href='/news_search.html?search_word={$vv}' title='' style='display:initial'><span>{$vv}</span></a>";
                    }
                    $data[$k]['label'] = $str;
                }
                $json = array('status'=>1,'data'=>$data);
                echo json_encode($json);exit;
            }else{
                $json = array('status'=>0,'data'=>'无');
                echo json_encode($json);exit;
            }
        }
    }

    function article(){
    	$aid = intval(input('route.id'));
    	if(!($aid>0)){
            $this->error('访问地址不存在','/');
        }
    	$one = Db::name('article')->where("is_del=0 AND is_hide=0 AND article_id={$aid}")->find();
    	if(!$one){
            $this->error('文章不存在','/');
        }
    	$kind = Db::name('article_kind')->where("kind_id=".$one['kind_id'])->find();

        if(strpos($kind['keywords'], '|')){
            $kind['hotword'] = explode('|', $kind['keywords']);
        }

        Db::name('article')->where("article_id=".$aid)->setInc('click');

        $this->assign('one',$one);
        $this->assign('kind',$kind);
        //处理分类
        $kind_data = Article::kind_data($kind['kind_id']);
        $top_kind = $kind_data['top_kind'];
        $two_kind = $kind_data['two_kind'];
        $two_kind_list = $kind_data['two_kind_list'];
        $three_kind = $kind_data['three_kind'];
        $three_kind_list = $kind_data['three_kind_list'];
        $this->assign('top_kind',$kind_data['top_kind']);
        $this->assign('two_kind_list',$kind_data['two_kind_list']);
        $this->assign('two_kind',$kind_data['two_kind']);
        $this->assign('three_kind',$kind_data['three_kind']);
        $this->assign('three_kind_list',$kind_data['three_kind_list']);
        //头部
        $this->assign('title',$one['title']);
        $this->assign('keywords',$one['keywords']);
        $this->assign('description',$one['description']);

        $this->assign('banner_img',$top_kind['banner_img']);
        $this->assign('p_banner_img',$top_kind['p_banner_img']);

        $top_kind = $kind_data['top_kind'];
        $two_kind = $kind_data['two_kind'];
        $three_kind = $kind_data['three_kind'];
        if($top_kind){
            $top_list = Db::name('article')->where('is_hide=0 and kind_id='.$top_kind['kind_id'])->order('sort_order,add_time desc')->select();
            $this->assign('top_list',$top_list);
        }
        if($two_kind){
            $two_list = Db::name('article')->where('is_hide=0 and kind_id='.$two_kind['kind_id'])->order('sort_order,add_time desc')->select();
            $this->assign('two_list',$two_list);
        }
        if($three_kind){
            $three_list = Db::name('article')->where('is_hide=0 and kind_id='.$three_kind['kind_id'])->order('sort_order,add_time desc')->select();
            $this->assign('three_list',$three_list);
        }
        if(!empty($kind['art_template'])){
        	return view($kind['art_template']);
        }else{
            $this->redirect("/");
        }
    }

    function plan(){
        $article_kid = config('article_kid');
        $plan_kind = db('articleKind')->where("kind_id=".$article_kid['plan'])->find();
        
        $this->assign("plan_kind",$plan_kind);
        
        if($plan_kind['web_title']){
            $title = $plan_kind['web_title'];
        }else{
            $title = $plan_kind['kind_name'].'-'.$this->config['site_name'];
        }
        $keywords = $plan_kind['keywords'];
        $description = $plan_kind['description'];
        $this->assign('title',$title);
        $this->assign('keywords',$keywords);
        $this->assign('description',$description);

        $top_kind['kind_id'] = 7;
        $this->assign("top_kind",$top_kind);
        //暂时
        $list = db('article')->where("is_hide=0 and kind_id=7")->order("sort_order,add_time desc")->limit(8)->select();
        $this->assign('list',$list);

        if(is_mobile()){
            return view('mobile/plan');
        }else{
            return view();
        }
        
    }


    function message(){
        if(request()->isAjax()){
            $inputData = input('post.');
            $code = $inputData['code'];
            if($code <> session('code')){
                $json = array('status'=>0,'msg'=>'参数错误');
                echo json_encode($json);exit;
            }
            $user_ip = getIP();
            if($user_ip){
                $limit = 3; //每天限制条数
                $user_message = db('message')->where(array('user_ip'=>$user_ip))->order('add_time desc')->limit($limit)->select();
                if($user_message){
                    $count_message = 0;
                    foreach($user_message as $k=>$v){
                        $add_time = $v['add_time'];
                        $start = strtotime(date('Y-m-d',time()));
                        $end   = strtotime(date('Y-m-d 23:59:59',time()));
                        if($add_time >= $start && $add_time <= $end){
                            $count_message++;
                        }
                    }
                    if($count_message >= $limit){
                        $json = array('status'=>0,'msg'=>'留言已达限制条数，请明天再来。');
                        echo json_encode($json);exit;
                    }
                }
            }
            $data['theme'] = $inputData['theme'];
            // $data['company'] = $inputData['company'];
            $data['formwho'] = $inputData['formwho'];
            // $data['sex'] = $inputData['sex'];
            // $data['tel'] = $inputData['tel'];
            // $data['mobile'] = $inputData['mobile'];
            // $data['weixin'] = $inputData['weixin'];
            $data['email'] = $inputData['email'];
            $data['content'] = $inputData['content'];
            // $data['address'] = $inputData['province'].$inputData['city'].$inputData['area'];
            $data['add_time'] = time();
            $data['user_ip'] = $user_ip;
            //dump($data);exit;
            $res = db('message')->insertGetId($data);
            if($res){
                $json = array('status'=>1,'msg'=>'提交成功，谢谢！');
                echo json_encode($json);exit;
            }else{
                $json = array('status'=>0,'msg'=>'提交失败');
                echo json_encode($json);exit;
            }
        }else{
            if(is_mobile()){
                $title = $keywords = $description = '教练预约-'.$this->config['site_name'];
                $this->assign('title',$title);
                $this->assign('keywords',$keywords);
                $this->assign('description',$description);
                return view('mobile/message');
            }
        }
    }

    function nav(){
        if(is_mobile()){
            $title = $keywords = $description = '导航菜单-'.$this->config['site_name'];
            $this->assign('title',$title);
            $this->assign('keywords',$keywords);
            $this->assign('description',$description);

            return view('mobile/nav');
        }
    }

    function search(){
        if(request()->isAjax()){
            $p = input('post.p');
            $condition = "kind_id in(34,35,46,55) AND is_hide=0 AND is_del=0";
            $search_word = input("post.search_word");
            if($search_word != ''){
                $condition .= " and (title like '%".$search_word."%' or map like '%".$search_word."%')";
            }
            $count = db('article')->where($condition)->count();// 查询满足要求的总记录数
            $num = 8;
            //保持分页搜索条件，paginate($num,$count,['query'=>request()->param()])
            $data = db('article')->where($condition)->order('sort_order,add_time desc')->limit($num*$p,$num)->select();
            // // var_dump($data);exit;
            if($data){
                foreach($data as $k=>$v){
                    $str = '';
                    $label = explode(',',$v['map']);
                    foreach($label as $kk=>$vv){
                        $str.="<a href='/news_search.html?search_word={$vv}' title='' style='display:initial'><span>{$vv}</span></a>";
                    }
                    $data[$k]['label'] = $str;
                }
                $json = array('status'=>1,'data'=>$data);
                echo json_encode($json);exit;
            }else{
                $json = array('status'=>0,'data'=>'无');
                echo json_encode($json);exit;
            }
        }else{
            $condition = "kind_id in(34,35,46,55) AND is_hide=0 AND is_del=0";
            $search_word = input("get.search_word");
            if($search_word != ''){
                $condition .= " and (title like '%".$search_word."%' or map like '%".$search_word."%')";
                $this->assign('search_word',$search_word);
            }
            $count = db('article')->where($condition)->count();// 查询满足要求的总记录数
            $num = 8;
            //保持分页搜索条件，paginate($num,$count,['query'=>request()->param()])
            $list = db('article')->where($condition)->order('sort_order,add_time desc')->paginate($num,$count,['query'=>request()->param()])->each(function($item,$key){
                return $item;
            });
            $page = $list->render();
            $one = db('article')->where($condition)->order('sort_order,add_time desc')->find();
            $this->assign('list',$list);
            $this->assign('page',$page);
            $this->assign('one',$one);

            $title = $keywords = $description = $search_word.'-'.$this->config['site_name'];
            $this->assign('title',$title);
            $this->assign('keywords',$keywords);
            $this->assign('description',$description);
            // dump($list);exit;
            if(is_mobile()){
                return view('mobile/news_search');
            }else{
                return view();
            }
            
        }
    }


    // function ajax_gongyi(){
    //     if(request()->isAjax()){
    //         $aid = intval(input('get.aid'));
    //         if(!$aid || !is_numeric($aid)) exit;
    //         $one = db('article')->where("article_id={$aid} AND is_hide=0")->find();
    //         if(!$one) exit;
    //         $this->assign('one',$one);

    //         $pre = db('article')->where("article_id > {$aid} AND is_hide=0 AND kind_id=".$one['kind_id'])->order('article_id')->find();
    //         $next = db('article')->where("article_id < {$aid} AND is_hide=0 AND kind_id=".$one['kind_id'])->order('article_id desc')->find();
    //         $this->assign('pre',$pre);
    //         $this->assign('next',$next);

    //         return view();
    //     }
    // }

    // //门店地图
    // function map_shop(){
    //     if(is_mobile()){
    //         $this->redirect('Index/category', array('kid' => 16));
    //     }
    //     $city_name = input('post.city_name');
    //     if(!$city_name || $city_name==''){
    //         $city_name = '广州市';
    //     }
    //     $shops = db('Shop')->select();
    //     foreach($shops as $k=>$v){
    //         $shop_data[$k]['id'] = $v['id'];
    //         $shop_data[$k]['Lng'] = $v['map_x'];
    //         $shop_data[$k]['Lat'] = $v['map_y'];
    //     }
    //     // // dump($shop_data);exit;
    //     $this->assign('city_name',$city_name);
    //     $this->assign('shop_data',json_encode($shop_data));

    //     $title = $keywords = $description = '全国体验店'.'-'.$this->config['site_name'];
    //     $this->assign('title',$title);
    //     $this->assign('keywords',$keywords);
    //     $this->assign('description',$description);
    //     return view();
    // }
    // //
    // function ajax_shop(){
    //     if(request()->isAjax()){
    //         $shop_id = input('post.shop_id');
    //         if($shop_id>0){
    //             $shop = db('Shop')->where("id=".$shop_id)->find();
    //             if($shop){
    //                 $json['status'] = 1;
    //                 $json['msg'] = '查询成功';
    //                 $json['data'] = $shop;
    //             }else{
    //                 $json['status'] = 0;
    //                 $json['msg'] = '查询无结果';
    //             }
    //             echo json_encode($json);exit;
    //         }else{
    //             $json['status'] = 0;
    //             $json['msg'] = '参数错误';
    //             echo json_encode($json);exit;
    //         }
    //     }
    // }

    // public function get_city($val=''){
    //     if(!request()->isAjax() || $val<1){
    //         $this->redirect('Index/category', array('kid' => 16));
    //     }
    //     $where['pid'] = $val;
    //     $res = db('City')->where($where)->select();
    //     if($res){
    //         $data['category'] = $res;
    //         echo json_encode($data);exit;
    //     }
    // }
    // //手机端shop列表
    // function search_shop(){
    //     $title = $keywords = $description = '经销商查询'.'-'.$this->config['site_name'];
    //     $this->assign('title',$title);
    //     $this->assign('keywords',$keywords);
    //     $this->assign('description',$description);
    //     if(is_mobile()){
    //         $city_id = input('post.city_id');
    //         if($city_id > 0){
    //             $where['city_id'] = $city_id;
    //             $shop_list = db('Shop')->where($where)->select();
    //             $this->assign('shop_list',$shop_list);
    //             return view('wap_shop_list');
    //         }
    //     }else{
    //         $this->redirect('Index/category', array('kid' => 16));
    //     }
    // }
    // //手机端shop详情
    // function get_shop(){
    //     if(is_mobile()){
    //         $id = input('route.id');
    //         if($id > 0){
    //             $where['id'] = $id;
    //             $shop = db("Shop")->where($where)->find();
    //             $pics = json_decode($shop['content'],true);
    //             $this->assign("shop",$shop);
    //             $this->assign("pics",$pics);

    //             $title = $keywords = $description = $shop['title'].'-'.$this->config['site_name'];
    //             $this->assign('title',$title);
    //             $this->assign('keywords',$keywords);
    //             $this->assign('description',$description);

    //             return view('map_shop');
    //         }
    //     }else{
    //         $this->redirect('Index/category', array('kid' => 16));
    //     }
    // }

    // //预约服务
    // function yuyue(){
    //     if(request()->isAjax()){
    //         $postData = input('post.');
    //         if($postData['formwho'] == ''){
    //             $json = array('status'=>0,'msg'=>'请输入用户名');
    //             echo json_encode($json);exit;
    //         }
    //         if($postData['mobile'] == ''){
    //             $json = array('status'=>0,'msg'=>'请输入手机号码');
    //             echo json_encode($json);exit;
    //         }
    //         $count = db('message')->where("mobile=".$postData['mobile'])->find();
    //         if($count>0){
    //             $json = array('status'=>0,'msg'=>'您已经预约过了');
    //             echo json_encode($json);exit;
    //         }
    //         if($postData['mobile_code'] == ''){
    //             $json = array('status'=>0,'msg'=>'请输入手机验证码');
    //             echo json_encode($json);exit;
    //         }
    //         // if($mobile_code != cookie('mobile_code')){
    //         //     $json = array('status'=>0,'msg'=>'短信验证码不正确');
    //         //     echo json_encode($json);exit;
    //         // }
    //         $data['formwho'] = $postData['formwho'];
    //         $data['mobile'] = $postData['mobile'];
    //         $data['theme'] = $postData['theme'];
    //         $data['address'] = $postData['Province'].$postData['City'].$postData['Area'].$postData['address'];
    //         $data['type'] = 2;
    //         $data['user_ip'] = getIp();
    //         $data['add_time'] = time();
    //         $res = db('message')->insertGetId($data);
    //         if($res){
    //             $json = array('status'=>1,'msg'=>'预约成功');
    //             echo json_encode($json);exit;
    //         }
    //     }else{
    //         $this->redirect("/");
    //     }
    // }

    // //短信验证码
    // function get_msg(){
    //     // $json = array("status"=>1,"msg"=>"发送成功");
    //     // echo json_encode($json);exit;
        
    //     $postData = input('post.');
    //     $username = $postData['username'];
    //     $mobile = $postData['tel'];
    //     $count = db('message')->where("mobile=".$postData['tel'])->find();
    //     if($count>0){
    //         $json = array('status'=>0,'msg'=>'您已经预约过了');
    //         echo json_encode($json);exit;
    //     }

    //     $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
    //     $mobile_code=rand(100000,999999);
    //     $smsConf = array(
    //         'key'   => '3589b813c5bd1a47d6c1e9ffb5ee24f1', //您申请的APPKEY
    //         'mobile'    => $mobile, //接受短信的用户手机号码
    //         'tpl_id'    => '98211', //您申请的短信模板ID，根据实际情况修改
    //         'tpl_value' =>'#code#='.$mobile_code.'&#company#=聚合数据' //您设置的模板变量，根据实际情况修改
    //     );
    //     cookie('mobile_code',$mobile_code,3600); // 指定cookie保存时间
    //     // $json = array('status'=>1,'msg'=>'短信发送成功','data'=>$result);
    //     //         echo json_encode($json);exit;
    //     $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
    //     if($content){
    //         $result = json_decode($content,true);
    //         $error_code = $result['error_code'];
    //         if($error_code == 0){
    //             //状态为0，说明短信发送成功
    //             // echo "短信发送成功,短信ID：".$result['result']['sid'];
    //             $json = array('status'=>1,'msg'=>'短信发送成功','data'=>$result);
    //             echo json_encode($json);exit;
    //         }else{
    //             //状态非0，说明失败
    //             // $msg = $result['reason'];
    //             $json = array('status'=>0,'msg'=>'短信发送失败','data'=>$result);
    //             echo json_encode($json);exit;
    //         }
    //     }else{
    //         //返回内容异常，以下可根据业务逻辑自行修改
    //         $json = array('status'=>0,'msg'=>'短信发送异常');
    //         echo json_encode($json);exit;
    //     }
    // }

    // /**
    //  * 请求接口返回内容
    //  * @param  string $url [请求的URL地址]
    //  * @param  string $params [请求的参数]
    //  * @param  int $ipost [是否采用POST形式]
    //  * @return  string
    //  */
    // function juhecurl($url,$params=false,$ispost=0){
    //     $httpInfo = array();
    //     $ch = curl_init();
    //     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    //     curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
    //     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
    //     curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
    //     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    //     if( $ispost )
    //     {
    //         curl_setopt( $ch , CURLOPT_POST , true );
    //         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
    //         curl_setopt( $ch , CURLOPT_URL , $url );
    //     }
    //     else
    //     {
    //         if($params){
    //             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
    //         }else{
    //             curl_setopt( $ch , CURLOPT_URL , $url);
    //         }
    //     }
    //     $response = curl_exec( $ch );
    //     if ($response === FALSE) {
    //         //echo "cURL Error: " . curl_error($ch);
    //         return false;
    //     }
    //     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    //     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    //     curl_close( $ch );
    //     return $response;
    // }


}
